package com.one.blocks.rbac.listener;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2022-12-08 09:21:17
 */
@Aspect
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class CacheClearAspect {

    @Autowired
    private ApplicationContext applicationContext;

    @Around("@annotation(com.one.blocks.rbac.listener.CacheEventPublisher)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        CacheEventPublisher eventPublisher = methodSignature.getMethod().getAnnotation(CacheEventPublisher.class);
        applicationContext.publishEvent(new CacheClearEvent(this, eventPublisher.value()));
        return result;
    }
}
